package pe.com.trener.layer.model.persistence.daoimpl;

/**
 * @author:   Oscar Erick Candela Carbajal
 * @rol:	  Analista Programador
 * @proyecto: Portal Trener
 *
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pe.com.trener.layer.model.persistence.dao.PlanillonDao;
import pe.com.trener.layer.model.pojo.Planillon;

public class PlanillonDaoImpl extends HibernateDaoSupport implements PlanillonDao {

	public void modificar(Planillon objectModel) throws Exception {		
		try {
			getHibernateTemplate().update(objectModel);
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		} 			
	}	
	
	@SuppressWarnings("unchecked")
	public List<Planillon> buscarPorCriterio(String codigoGradoSeccion, Integer codigoArea, Integer codigoTrimestre) throws Exception {
		try {
	        List<Planillon> resultQuery = new ArrayList<Planillon>();
		 	HibernateTemplate ht = getHibernateTemplate();
  
		 	Object[] values = new Object[3];
	        values[0] = codigoGradoSeccion;
	        values[1] = codigoArea;
	        values[2] = codigoTrimestre;
	        
	        String queryString = " from " +
								 " Planillon o " +
								 " where " +
	        					 "     o.protocoloArea.gradoSeccion.codigoGradoSeccion = ? "+
	        					 " and o.protocoloArea.area.codigoArea = ? "+
	        					 " and o.trimestre.codigoTrimestre = ? ";	        
	        
	        resultQuery = (List<Planillon>)ht.find(queryString, values);
	        
	        return resultQuery;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}
	
	@SuppressWarnings("unchecked")
	public List<Planillon> buscarPorCriterio02(String codigoGradoSeccion, Integer codigoArea, Integer codigoTrimestre, String codigoAnio) throws Exception {
		try {
	        List<Planillon> resultQuery = new ArrayList<Planillon>();
		 	HibernateTemplate ht = getHibernateTemplate();
  
		 	Object[] values = new Object[4];
	        values[0] = codigoGradoSeccion;
	        values[1] = codigoArea;
	        values[2] = codigoTrimestre;
	        values[3] = codigoAnio;
	        
	        String queryString = " from " +
								 " Planillon o " +
								 " where " +
	        					 "     o.protocoloArea.gradoSeccion.codigoGradoSeccion = ? "+
	        					 " and o.protocoloArea.area.codigoArea = ? "+
	        					 " and o.trimestre.codigoTrimestre = ? " +
	        					 " and o.trimestre.anioAcademico.codigoAnio = ?";	        
	        
	        resultQuery = (List<Planillon>)ht.find(queryString, values);
	        
	        return resultQuery;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}	
	
	@SuppressWarnings("unchecked")
	public Planillon buscarPorPk(Integer parametroPk) throws Exception {
		try {
	        List<Planillon> resultQuery = new ArrayList<Planillon>();
	        HibernateTemplate ht = getHibernateTemplate();
	        
	        Planillon objectModel = new Planillon();
	        		 	
		 	Object[] values = new Object[1];
	        values[0] = parametroPk;
  
	        String queryString = " from " +
								 " Planillon o " +
								 " where " +
								 " 	  (o.codigoPlanillon = ?) ";
	        
	        resultQuery = ht.find(queryString, values);
	        
	        objectModel = (Planillon)DataAccessUtils.uniqueResult(resultQuery);
	        
	        return objectModel;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}	
	
}